#!/bin/bash

# 部署脚本
echo "开始构建和部署过程..."

# 1. 构建前端
echo "构建前端..."
cd frontend
npm install
npm run build
if [ $? -ne 0 ]; then
  echo "前端构建失败，退出部署"
  exit 1
fi
cd ..

# 2. 打包项目
echo "打包项目..."
rm -f app_dashboard.tar.gz
tar -czf app_dashboard.tar.gz frontend/build backend package.json

# 3. 上传到服务器
echo "上传到服务器..."
scp app_dashboard.tar.gz root@47.116.122.58:/home/

# 4. 远程部署
echo "在服务器上部署..."
ssh root@47.116.122.58 << 'ENDSSH'
cd /home
tar -xzf app_dashboard.tar.gz -C /home/app_dashboard
cd /home/app_dashboard/backend
npm install
pm2 restart app_dashboard || pm2 start server.js --name app_dashboard
echo "部署完成"
ENDSSH

echo "本地构建和部署完成！" 